package com.ymatou.api.sellerpromotion.testcase;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.api.marketingseller.service.CreateSellerPromotionCall;
import com.ymatou.api.sellerpromotion.service.GetPromotionProductByIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.SellerPromotionCallService;
import com.ymttest.database.mongo.SellerPromotionInfoWapper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;



public class Ts_GetPromotionProductById {
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据促销ID、买手ID获取促销商品信息");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetPromotionProductByIdCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionProductById_001() {
		Logger.start(true, "获取进行中-指定商品促销中的商品ID，商品ID与mongo一致");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满2件减10-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","1");
			map.put("PromotionInfo->IsSpecifyProduct",true);
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
//			
//			// 促销商品ID
//			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
//			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
//			JSONObject GetPromotionProductByIdBean = new GetPromotionProductByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", promotionId));
//			Logger.verifyEquals(200,GetPromotionProductByIdBean.getInt("Code"), "响应报文-返回码");			
//			// 校验商品ID是否正确
//			JSONArray productIds = new JSONArray(mongoDataMap.get("ProductIds").toString());
//			JSONArray prodSqlArray = GetPromotionProductByIdBean.getJSONObject("Data").getJSONArray("ProductIds");
//			AssertService.checkArray(productIds,prodSqlArray);
//			// 获取满减促销活动的商品类型
//			int productType =  GetPromotionProductByIdBean.getJSONObject("Data").getInt("ProductType");
//			Logger.verifyEquals(2, productType, "满减促销活动的商品类型为：指定商品促销");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionProductById_002() {
		Logger.start(true, "获取进行中-排除商品促销中的商品ID，商品ID与数据库一致");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满2件减10-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","1");
			map.put("PromotionInfo->IsLimitProduct",true);
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			
			// 促销商品ID
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
			JSONObject GetPromotionProductByIdBean = new GetPromotionProductByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", promotionId));
			Logger.verifyEquals(200,GetPromotionProductByIdBean.getInt("Code"), "响应报文-返回码");			
			// 校验商品ID是否正确
			JSONArray productIds = new JSONArray(mongoDataMap.get("ProductIds").toString());
			JSONArray prodSqlArray = GetPromotionProductByIdBean.getJSONObject("Data").getJSONArray("ProductIds");
			AssertService.checkArray(productIds,prodSqlArray);
			// 获取满减促销活动的商品类型
			int productType =  GetPromotionProductByIdBean.getJSONObject("Data").getInt("ProductType");
			Logger.verifyEquals(3, productType, "满减促销活动的商品类型为：排除商品促销");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionProductById_003() {
		Logger.start(true, "获取进行中-全场商品促销中的商品ID，商品ID查询结果为空");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满2件享8.8-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","1");
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			
			// 促销商品ID
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
			JSONObject GetPromotionProductByIdBean = new GetPromotionProductByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", promotionId));
			Logger.verifyEquals(200,GetPromotionProductByIdBean.getInt("Code"), "响应报文-返回码");
			// 校验商品ID是否正确
			JSONArray productIds = new JSONArray(mongoDataMap.get("ProductIds").toString());
			JSONArray prodSqlArray = GetPromotionProductByIdBean.getJSONObject("Data").getJSONArray("ProductIds");
			AssertService.checkArray(productIds, prodSqlArray);
			// 获取满减促销活动的商品类型
			int productType =  GetPromotionProductByIdBean.getJSONObject("Data").getInt("ProductType");
			Logger.verifyEquals(1, productType, "满减促销活动的商品类型为：全场商品促销");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionProductById_004() {
		Logger.start(false, "获取未开始满减促销中的商品ID,查询结果为空");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满2件享8.8-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(1));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","1");
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			// 促销商品ID
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
			int  PromotionState = (int) mongoDataMap.get("PromotionState");
			if(PromotionState==0){	
			} else{
				return;
			}
			JSONObject GetPromotionProductByIdBean = new GetPromotionProductByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", promotionId));
			Logger.verifyEquals(200,GetPromotionProductByIdBean.getInt("Code"), "响应报文-返回码");
			Logger.verifyIsNull(GetPromotionProductByIdBean.getString("Data"), "查询结果为空");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionProductById_005() {
		Logger.start(false, "获取已结束满减促销中的商品ID,查询结果为空");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满2件享8.8-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(1));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","1");
			map.put("PromotionInfo->IsLimitProduct",true);
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			//取消活动
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			// 促销商品ID
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
			Logger.verifyIsNull(mongoDataMap, "mongo 中没有数据");
			JSONObject GetPromotionProductByIdBean = new GetPromotionProductByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"promotionId", promotionId));
			Logger.verifyEquals(200,GetPromotionProductByIdBean.getInt("Code"), "响应报文-返回码");
			Logger.verifyIsNull(GetPromotionProductByIdBean.getString("Data"), "查询结果为空");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
